<?php
//check1
namespace Home\Controller;
use Think\Controller;
use Think\Model;
use Home\Model\AbasicsModel;

class AbasicsController extends Controller {
    public function index(){
    	if(session('?user')){
    		$this->assign('user',I('session.user'));   		   	
    		$reserve = M('Abasics');
    		$data1['email'] = I('session.user');
    		$data1['submit'] = '0';
    		$data2['email'] = I('session.user');
    		$data2['submit'] = '1';
    		$result1=$reserve->where($data1)->select();
    		$result2=$reserve->where($data2)->select();
    		if($result1){
    			$this->assign('data', $result1[0]);
    			$this->display();
    		}	
    		else if($result2)
    			redirect(U('Myhomepage/index'));
    		else{
    			$this->display(); 
    		}  		
    	} 	
    	else
    		redirect(U('Login/index'));
    }
    
    public function save(){
    	if(IS_POST&&session('?user')){
	    	$table = D('Abasics');
	    	$data['email']=session('user');
	    	$data['birth'] = I('post.birth');
	    	$data['gender'] = I('post.gender');
	    	$data['passport'] = I('post.passport');
	    	$data['idcard'] = I('post.idcard');
	    	$data['nationality'] = I('post.nationality');
	    	$data['dual_nationality'] = I('post.dual_nationality');
	    	$data['residence'] = I('post.residence');	    	
	    	$data['gre'] = I('post.gre');
	    	$data['gmat'] = I('post.gmat');
	    	$data['ielts'] = I('post.ielts');
	    	$data['toefl'] = I('post.toefl');
	    	$data['university'] = I('post.university');
	    	$data['department'] = I('post.department');
	    	$data['enrollment_date'] = I('post.enrollment_date');	
	    	$data['major'] = I('post.major');
	    	$data['gpa'] = I('post.gpa');	
	    	$data['gpa_total'] = I('post.gpa_total');
	    	$data['loan'] = I('post.loan');
	    	$data['tuition_total'] = I('post.tuition_total');
	    	$data['tuition_date'] = I('post.tuition_date');	  
	    	$data['loan_duration'] = I('post.loan_duration');
	    	$data['currency'] = I('post.currency');
	    	if ($data1=$table->create($data)){
	    		$user=I('session.user');
	    		$change=$table->where($data1)->select();
	    		$result=$table->where("email='$user'")->select();
	    		if($change){
	    			redirect(U('Acontact/index'));
	    		}
	    		else{
		    		if($result){
		    			if($table->save($data1))
		    				redirect(U('Acontact/index'));
		    			else{
		    				$error=$table->getError();
		    				$this->assign('message','保存失败');
		    				$this->assign('title1','保存失败');
		    				$this->assign('pagename','申请页面');
		    				$this->assign('data',$error);
		    				$this->error('保存失败！','index',3);
		    			}
		    		}
		    		else{	    			
			    		if($table->add($data1))
			    			redirect(U('Acontact/index'));
			    		else{
			    			$error=$table->getError();
			    			$this->assign('message','保存失败');
			    			$this->assign('title1','保存失败');
			    			$this->assign('pagename','申请页面');
			    			$this->assign('data',$error);
			    			$this->error('保存失败！','index',3);		
			    		}
		    		}
		    	}
	    	}
	    	else{
	    		$error=$table->getError(); 
	    		$this->assign('message','保存失败');
	    		$this->assign('title1','保存失败');
	    		$this->assign('pagename','申请页面');
	    		$this->assign('data',$error);
	    		$this->error('保存失败！','index',3);
	    	} 
    	}
    	else
    		$this->display('Login/index');  		    	
    }
}